package app.view.mediators
{
	import app.DefaultPaths;
	
	import flash.events.Event;
	
	import mx.events.ModuleEvent;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class AirShellMediator extends Mediator
	{
		public static const NAME:String = "AirShellMediator";
		public static const LOAD_MODULE:String = "airshell/module/load";
		public static const LOAD_MODULE_FAILURE:String = "airshell/module/failure";
		public static const LOAD_MODULE_READY:String = "airshell/module/ready";
		
		
		
		public function AirShellMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);			
		}
		
		override public function onRegister():void{
			initiateView();
		}
		override public function listNotificationInterests():Array{
			return [ LOAD_MODULE ];
		}
		
		override public function handleNotification(note:INotification):void{
			switch(note.getName()){
				case LOAD_MODULE:
					loadModule();
					break;
			}
		}
		
		
		private function loadModule():void{
			view.loader.addEventListener(ModuleEvent.READY, onModuleReady);
			view.loader.addEventListener(ModuleEvent.ERROR, onModuleError);
			//view.loader.applicationDomain = new ApplicationDomain( ApplicationDomain.currentDomain );
			view.loader.url = DefaultPaths.FSMVIZ_MODULE;
		}
		
		private function onModuleReady(event:Event):void{
			sendModuleReadyNote(event);
			
		}
		
		
		
		
		private function onModuleError(event:Event):void{
			sendModuleErrorNote(event);
		}
		
		private function sendModuleErrorNote(event:Event):void{
			sendNotification( LOAD_MODULE_FAILURE, event);
		}
		
		private function sendModuleReadyNote(event:Event):void{
			sendNotification( LOAD_MODULE_READY, event);
		}
		
		protected function initiateView():void{
			createChildMediators();
		}

		private function createChildMediators():void{
			var loaderMediator:Mediator = new ModuleLoaderMediator( );
			loaderMediator.setViewComponent( view.loader );
			facade.registerMediator( loaderMediator );
			
			facade.registerMediator( new FileMediator());
		}
		
		private function get view():AirShell{
			return AirShell( viewComponent );
		}
		
	}
}